package com.erp.erp_mask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.erp_mask.pojo.TCategory;
import com.erp.erp_mask.service.TCategoryService;
import com.erp.erp_mask.mapper.TCategoryMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang
 * @description 针对表【t_category】的数据库操作Service实现
 * @createDate 2024-05-24 15:04:01
 */
@Service
public class TCategoryServiceImpl extends ServiceImpl<TCategoryMapper, TCategory>
        implements TCategoryService {

    public List<TCategory> findlist(Integer level) {
        LambdaQueryWrapper<TCategory> wrapper = Wrappers.lambdaQuery();
        if (level == null) {
            wrapper.isNull(TCategory::getParentLevel);
        } else {
            wrapper.eq(TCategory::getParentLevel, level);
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<TCategory> searchlist() {
        List<TCategory> findwhered = this.findlist(null);
        Map<Integer, TCategory> categoryMap = new HashMap<>();
        for (TCategory cat : findwhered) {
            categoryMap.put(cat.getCatId(), cat);
        }
        for (TCategory cat : findwhered) {
            List<TCategory> children = this.findlist(cat.getCatId());
            cat.setChildren(children);
            for (TCategory child : children) {
                List<TCategory> grandChildren = this.findlist(child.getCatId());
                child.setChildren(grandChildren);
            }
        }
        return findwhered;
    }
}




